<template>
  <div class="tableAction">
    <div class="flex items-center justify-center">
      <template v-for="(action, index) in getActions" :key="`${index}-${action.label}`">
        <template v-if="!action.popConfirm">
          <a-button v-bind="action.btnProps" :class="{ 'mr-2': action.type != 'link' }">
            <template #icon v-if="action.icon">
              <component :is="action.icon" class="arco-icon" />
            </template>
            {{ action.label }}
          </a-button>
        </template>
        <template v-else>
          <a-popconfirm v-bind="action.popConfirm">
            <a-button class="mr-2" v-bind="action.btnProps">
              <template #icon v-if="action.icon">
                <component :is="action.icon" class="arco-icon" />
              </template>
              {{ action.label }}
            </a-button>
            <template #content>{{ action.content }}</template>
          </a-popconfirm>
        </template>
      </template>
      <a-dropdown v-if="dropDownActions && getDropdownList.length">
        <slot name="more"></slot>
        <template #content>
          <a-doption
            v-for="item in getDropdownList"
            :key="item.key"
            @click="item.onClick && item.onClick(item.key)"
            >{{ item.label }}</a-doption
          >
        </template>
        <a-button v-bind="getMoreProps">
          <template #icon v-if="getMoreProps.icon">
            <component :is="getMoreProps.icon" class="arco-icon" />
          </template>
          {{ getMoreProps.label }}
        </a-button>
      </a-dropdown>
    </div>
  </div>
</template>

<script lang="ts">
  import { defineComponent, PropType, computed, toRaw } from 'vue';
  import { ActionItem, DropDownItem } from '@/components/Table';
  import { usePermission } from '@/hooks/web/usePermission';
  import { isBoolean, isFunction } from '@/utils/is';
  import { DownOutlined } from '@vicons/antd';
  import { omit } from 'lodash-es';

  export default defineComponent({
    name: 'TableAction',
    components: { DownOutlined },
    props: {
      actions: {
        type: Array as PropType<ActionItem[]>,
        default: null,
        required: true,
      },
      dropDownActions: {
        type: Array as PropType<ActionItem[]>,
        default: null,
      },
      style: {
        type: String as PropType<String>,
        default: 'button',
      },
      select: {
        type: Function as PropType<Function>,
        default: () => {},
      },
      dropDownProps: {
        type: Object as PropType<DropDownItem>,
        default: () => {},
      },
    },
    setup(props) {
      const { hasPermission } = usePermission();

      const getMoreProps = computed(() => {
        return {
          ...(props.dropDownProps || {}),
        };
      });

      const getDropdownList = computed(() => {
        return (toRaw(props.dropDownActions) || [])
          .filter((action) => {
            return hasPermission(action.auth as string[]) && isIfShow(action);
          })
          .map((action) => {
            return {
              ...action,
            };
          });
      });

      function isIfShow(action: ActionItem): boolean {
        const ifShow = action.ifShow;

        let isIfShow = true;

        if (isBoolean(ifShow)) {
          isIfShow = ifShow;
        }
        if (isFunction(ifShow)) {
          isIfShow = ifShow(action);
        }
        return isIfShow;
      }

      const getActions = computed(() => {
        return (toRaw(props.actions) || [])
          .filter((action) => {
            return hasPermission(action.auth as string[]) && isIfShow(action);
          })
          .map((action) => {
            const { popConfirm } = action;
            const btnProps = omit(action, ['popConfirm']);
            //需要展示什么风格，自己修改一下参数
            return {
              ...action,
              ...(popConfirm || {}),
              btnProps,
              onOk: popConfirm?.onOk,
              onCancel: popConfirm?.onCancel,
              enable: !!popConfirm,
            };
          });
      });

      return {
        getActions,
        getDropdownList,
        getMoreProps,
      };
    },
  });
</script>
